package com.iot.ammeter.controller.android.appUser;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.app.register.AppUserRegisterDTO;
import com.iot.ammeter.dto.app.user.AppUserInfoDTO;
import com.iot.ammeter.dto.app.user.AppUserListDTO;
import com.iot.ammeter.dto.app.user.AppUserLoginDTO;
import com.iot.ammeter.query.android.AppUserQuery;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.android.user.AppUserService;
import com.iot.ammeter.service.other.SendEmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * app端用户表 前端控制器
 * </p>
 *
 * @author yjx
 * @since 2021-10-18
 */
@RestController
@Api(tags = {"app端用户管理"})
@RequestMapping("/api/app_user")
public class AppUserController {

    private final AppUserService appUserService;
    private final SendEmailService sendEmailService;

    public AppUserController(AppUserService appUserService, SendEmailService sendEmailService) {
        this.appUserService = appUserService;
        this.sendEmailService = sendEmailService;
    }

    /**
     * app端用户注册
     *
     * @param appUserRegisterDTO 用户注册传输对象
     * @return
     */
    @ApiOperation("app端用户注册")
    @LogAnnotation(module = "appUserRegister", operator = "app端用户注册")
    @PostMapping("/register")
    public ResponseResult webUserRegister(@RequestBody AppUserRegisterDTO appUserRegisterDTO) {
        Map<String, String> data = appUserService.appUserRegister(appUserRegisterDTO);
        return data.get("token") != null ? ResponseResult.SUCCESS(data.get("token")) : ResponseResult.FAILED(data.get("fail"));
    }

    /**
     * 判断邮箱是否已被注册
     *
     * @param email 邮箱号
     * @return ResponseResult
     */
    @ApiOperation("判断电子邮箱是否被注册")
    @LogAnnotation(module = "appEmailIsExists", operator = "判断电子邮箱是否被注册")
    @GetMapping("/email_is_exists/{email}")
    public ResponseResult emailIsExists(@PathVariable String email) {
        return appUserService.appEmailIsExists(email);
    }

    /**
     * 发送注册邮箱验证码
     *
     * @param email 邮箱号
     * @return
     */
    @ApiOperation("发送注册验证码")
    @LogAnnotation(module = "sendEmailCaptcha", operator = "判断电子邮箱是否被注册")
    @PostMapping("/sendemail/{email}")
    public ResponseResult sendEmail(@PathVariable String email) {
//        return sendEmailService.appSendEmailCaptcha(email,"1");
        sendEmailService.appSendEmailCaptcha(email, "1");
        return ResponseResult.SUCCESS();
    }

    /**
     * app端用户登录
     *
     * @param appUserLoginDTO app端用户传输对象DTO
     * @return
     */
    @ApiOperation("app用户登录")
    @LogAnnotation(module = "appUserLogin", operator = "app用户登录")
    @PostMapping("/appUserLogin")
    public ResponseResult appUserLogin(@RequestBody AppUserLoginDTO appUserLoginDTO) {
        Map<String, String> date = appUserService.appUserLogin(appUserLoginDTO);
        return date.get("token") != null ? ResponseResult.SUCCESS(date.get("token")) : ResponseResult.FAILED(date.get("fail"));
    }

    /**
     * app用户账户信息
     *
     * @param email app用户邮箱
     * @return
     */
    @ApiOperation("app用户信息查询")
    @LogAnnotation(module = "appUserInfo", operator = "app用户信息查询")
    @PostMapping("/appUserInfo/{email}")
    public ResponseResult appUserInfo(@PathVariable String email) {
        AppUserInfoDTO appUserInfoDTO = appUserService.selectAppUserByEmail(email);
        return appUserInfoDTO != null ? ResponseResult.SUCCESS(appUserInfoDTO) : ResponseResult.FAILED("无此用户");
    }


    /**
     * 获取android用户列表
     *
     * @param page         页码
     * @param pageSize     每页数量
     * @param appUserQuery 查询条件
     * @return android用户列表json数据
     */
    @ApiOperation("获取appUser列表")
    @LogAnnotation(module = "listAppUsers", operator = "获取appUser列表")
    @PostMapping("/list/{page}/{pageSize}")
    public ResponseResult listAppUsers(@PathVariable int page,
                                       @PathVariable int pageSize,
                                       @RequestBody(required = false) AppUserQuery appUserQuery) {
        if (page < 0 || pageSize < 5) {
            return ResponseResult.FAILED("页码参数有误");
        }
        IPage<AppUserListDTO> iPage = appUserService.listAppUser(page, pageSize, appUserQuery);
        return iPage.getRecords().size() > 0 ? ResponseResult.SUCCESS(iPage) : ResponseResult.SUCCESS("无数据");
    }
}
